﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace HotelBooking
{
    public class HotelSuplier
    {
        
        public int PriceMax = 10;
        private Random r;
        private OrderObject order;
        public int id { get; private set; }
        public int p { get; private set; }
        public float BasePriceOrder { get; private set; }
        public float RoomPrice { get; private set; }
        public int numOfRoom { get; private set; }
        public int numOfRoomsAvailable { get; private set; }
        float amount_charge;
        
        //delegate 
        public delegate void PromotionHandler(float newPrice, int ID, int roomsAvailable);
        public event PromotionHandler PriceHasDecreased;

        //delegate 
        public delegate void SuccessHandler();
        public event SuccessHandler sendLog;


        public HotelSuplier(int id, int numOfRooms, int numOfRoomsAvailable, float roomPrice )
        {
            this.id = id;
            this.order = new OrderObject();
            order.Amount = 10;
            r = new Random();
            this.numOfRoom = numOfRooms;
            this.numOfRoomsAvailable = numOfRoomsAvailable;
            this.RoomPrice = roomPrice;
        }

        //event of change of price 
        protected void OnLog(float newPrice, int IDSender, int RoomsAvailable)
        {
            if (PriceHasDecreased != null)
            {
                PriceHasDecreased(newPrice, IDSender, RoomsAvailable);
            }
        }


        public void PricingModel()
        {
            float valueBasedRooms = 0f;
            if (numOfRoom > 0)
            {
                if (numOfRoomsAvailable / numOfRoom <= 0.1f)
                {
                    valueBasedRooms = 10;

                }
            }

            //new price based on orderm amount, random number, and Rooms availables. 
            if (order != null && r != null)
            {
                float newPrice = order.Amount * r.Next(PriceMax) + valueBasedRooms;

                if (newPrice < RoomPrice)
                {
                    OnLog(newPrice, id, numOfRoomsAvailable);

                }
                RoomPrice = newPrice;
            }


        }


        //receive the string order
        public void ReceiveOrder(string order)
        {
            this.order = Decoder.ConvertOrderString(order);
            Thread th = new Thread(OrderProcessingThread);
            Thread.Sleep(r.Next(0, 6000));
            th.Start();
        }


        public void OrderProcessingThread()
        {
            if (ProcessOrder(this.order) && sendLog != null)
            {
               
                sendLog();
                
            }
        }

        public bool ProcessOrder(OrderObject order)
        {
            bool ProcessSuccess = false;
            if (order.ReceiverID != 0)
            {
                float tax = 40.00f;
                float locationCharge = 20.00f;
             
                if (ValidCardNumber(order.CardNo))
                {
                    amount_charge = order.Amount * this.RoomPrice + tax + locationCharge;
                    ProcessSuccess = true;
                  
                        Program.Log += "\n     " + order.SendId + ":  "  + amount_charge.ToString();
                }
                else
                {
                    throw new Exception("Card No exception. It must be between 5000 and 7000");
                }
            }
            return ProcessSuccess;
        }



      
        public static bool ValidCardNumber(int cardNumber)
        {
            return cardNumber > 5000 && cardNumber < 7000;
        }


    }


}
